package com.ly.mpayment.persist.service.userloginlog;

import java.io.Serializable;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.UserLoginLog;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

/**
 *
 *
 *
 */
@Service
public class UserLoginLogServiceImpl extends AbstractService<UserLoginLog, Serializable> implements IUserLoginLogService{

	@Override
	public boolean addUserLoginLog(UserLoginLog log) {
		if(log != null){
			this.saveOrUpdate(log);
			return true;
		}
		return false;
	}

	@Override
	public Page<UserLoginLog> findPageLog(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select u from UserLoginLog u left join fetch u.user where 1 = 1  #[ and u.loginip = :loginip]#  #[ and u.user.username = :username]#  ";

		if(parameters.containsKey(":starttime")){
			hql += " and DATE_FORMAT(u.logintime,'%Y-%m-%d') >= '"+parameters.get(":starttime").toString().concat("' ");
			parameters.remove(":starttime");
		}
		if(parameters.containsKey(":endtime")){
			hql += " and DATE_FORMAT(u.logintime,'%Y-%m-%d') <= '"+parameters.get(":endtime").toString().concat("' ") ;
			parameters.remove(":endtime");
		}
		hql+=" order by u.logintime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public UserLoginLog findById(String uuid) {
		return this.findUnique("select u from UserLoginLog u where u.logsuuid = ?", uuid);
	}

}
